from common.connect_db import MyDB


class DataBaseFinance:

    @staticmethod
    def get_finance_account(account_id, account_type):
        """
        通过资产ID和资产类型获得用户资产详情
        :param account_id:
        :param account_type: 1、会员；2、商户；3、供应商；4代理商
        :return:
        """
        my_db = MyDB()
        sql = "select * from finance_account where id = '%s' and account_type ='%s'" % (account_id, account_type)
        result = my_db.execute_select(db_name='finance', sql_text=sql)
        if len(result) > 0:
            return result[0]
        else:
            raise ValueError("查找不到对应资产账户")

    @staticmethod
    def assert_finance_account(account_id, balance, account_type, today_balance_amount, today_balance_day,
                               today_withdraw_amount, frozen_amount, commission_rate, deleted, remark):
        """
        断言用户资产信息
        :param account_id:
        :param balance:
        :param account_type:
        :param today_balance_amount:
        :param today_balance_day:
        :param today_withdraw_amount:
        :param frozen_amount:
        :param commission_rate:
        :param deleted:
        :param remark:
        :return: True or False
        """
        my_db = MyDB()
        sql = "select * from finance_account " \
              "where id = '%s' and balance ='%s' " \
              "and account_type ='%s' and today_balance_amount ='%s' " \
              "and today_balance_day ='%s' and today_withdraw_amount ='%s' " \
              "and frozen_amount ='%s' and commission_rate ='%s' " \
              "and deleted ='%s' and remark ='%s' " % (
                  account_id, balance, account_type, today_balance_amount, today_balance_day,
                  today_withdraw_amount, frozen_amount, commission_rate, deleted, remark)
        result = my_db.execute_select(db_name='finance', sql_text=sql)
        if len(result) == 1:
            return True
        else:
            return False

    @staticmethod
    def add_order(order_sn,charge_id):
        my_db = MyDB()
        sql = "INSERT INTO `common_pay_charge_order`(ordersn,charge_id,order_id,status,amount,channel," \
              "transaction_no,is_notify,update_time,create_time,notify_url) " \
              "VALUES ('%s', '%s', NULL, NULL, 2500, 'alipay', '1591074032'," \
              " 0, now(), now()," \
              " 'http://api.hd.shall-buy.top/order/inner/order/callback')" % (order_sn,charge_id)
        print(sql)
        my_db.execute_alter(db_name='finance', sql_text=sql)

    @staticmethod
    def add_charge_order(order_sn, charge_id):
        my_db = MyDB()
        sql = "INSERT INTO common_pay_charge_order " \
              "(ordersn,charge_id,order_id,status,amount,channel,transaction_no," \
              "is_notify,update_time,create_time,notify_url) " \
              "VALUES ('%s', '%s', NULL, NULL, 2500, 'alipay', '1591074032', 0, now(), now()," \
              " 'http://api.hd.shall-buy.top/order/inner/order/callback')" % (order_sn, charge_id)
        my_db.execute_alter('finance', sql)

    @staticmethod
    def add_charge_order_order(order_sn, charge_id, order_id):
        my_db = MyDB()
        sql = "INSERT INTO common_pay_charge_order " \
              "(ordersn,charge_id,order_id,status,amount,channel,transaction_no," \
              "is_notify,update_time,create_time,notify_url) " \
              "VALUES ('%s', '%s', '%s', NULL, 2500, 'alipay', '1591074032', 0, now(), now()," \
              " 'http://api.hd.shall-buy.top/order/inner/order/callback')" % (order_sn, charge_id, order_id)
        my_db.execute_alter('finance', sql)

    @staticmethod
    def add_refund_charge(order_sn, refund_id):
        my_db = MyDB()
        sql = "select a.refund_sn from order.life_order_refund as a,order.life_order_order as b " \
              "where a.order_id=b.id and b.ordersn='%s' order by a.created_at desc limit 1" % order_sn
        result = my_db.execute_select('order', sql)
        if len(result) == 1:
            refund_sn = result[0].refund_sn
            sql = "INSERT INTO `finance`.`common_pay_refund`(`refund_id`, `charge_id`, `amount`, `status`," \
                  " `pay_channel`, `ordersn`, `refund_order_no`, `transaction_no`, `is_notify`, `create_time`," \
                  " `update_time`, `notify_url`, `remark`, `description`) " \
                  "VALUES ('%s', 'ch_101200408514816368640008'," \
                  " 10, 'pending', 'alipay', '%s', '%s', NULL, NULL," \
                  " now(), now(), 'http://api.t.shall-buy.top/order/inner/refund/callback', " \
                  "'', '{\"isCloseOrder\":\"1\",\"refundSn\":\"512004081358196530\"}')" % (
                  refund_id, order_sn, refund_sn)
            my_db.execute_alter('finance', sql)
        else:
            raise ValueError('查询不到这笔订单的退款信息')


if __name__ == '__main__':
    test = DataBaseFinance()
    test.add_charge_order(111, 2222)